package web.pre;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Easybuy_news;
import entity.Easybuy_product;
import entity.Easybuy_product_category;
import service.EasyBuyNewsService;
import service.EasyBuyProductCategoryService;
import service.EasyBuyProductService;
import service.Impl.EasyBuyNewsServiceImpl;
import service.Impl.EasyBuyProductCategoryServiceImpl;
import service.Impl.EasyBuyProductServiceImpl;

/**
 * Servlet implementation class ProductCategory
 */
public class ProductCategory extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		EasyBuyProductCategoryService service=new EasyBuyProductCategoryServiceImpl();
		
		String action=request.getParameter("action");
		
		List<Easybuy_product_category> list=service.list();
		request.getSession().setAttribute("category", list);
		
		
		EasyBuyProductService productService=new EasyBuyProductServiceImpl();
		Map<Integer, List<Easybuy_product>> pMap=new HashMap<>();
		for (Easybuy_product_category category : list) {
			if (category.getType()==1) {
				List<Easybuy_product> pList=productService.products(category.getId(),1,1,6).getList();
				
				pMap.put(category.getId(), pList);
			}
		}
		
		request.setAttribute("pmap", pMap);
		EasyBuyNewsService service1 =new EasyBuyNewsServiceImpl();
		List<Easybuy_news> news=service1.list();
		request.getSession().setAttribute("newsindex1", news);
		
		if (action.equals("index")) {
			request.getRequestDispatcher("Index.jsp").forward(request, response);
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
